﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Mss.UiCommon.Controls.TopMenu
{
    public class TopMenuControl : ItemsControl
    {
        #region Constructor

        static TopMenuControl()
        {
            //Apply default style.
            DefaultStyleKeyProperty.OverrideMetadata(typeof (TopMenuControl),
                                                     new FrameworkPropertyMetadata(typeof (TopMenuControl)));
        }

        #endregion

        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);

            if (Items == null) return;

            foreach (TopMenuItem item in Items.Cast<TopMenuItem>())
            {
                item.Checked += onItemChecked;
            }
        }

        private void onItemChecked(object sender, RoutedEventArgs e)
        {
            IEnumerable<TopMenuItem> menuItems =
                Items.Cast<TopMenuItem>().Except(new List<TopMenuItem> {(TopMenuItem) sender});
            foreach (TopMenuItem item in menuItems)
            {
                item.IsChecked = false;
            }
        }
    }
}